#include "precompiled.h"
#include "app_Tree.h"

using namespace app;

// TSystemTables //////////////////////////////////////////////////////////////

const prs::TTableId TSystemTables::Explorer  = "System_Explorer";
const prs::TTableId TSystemTables::Folders   = "System_Folders";
const prs::TTableId TSystemTables::RegTables = "System_RegTables";

// TTreeEvent /////////////////////////////////////////////////////////////////

DEFINE_EVENT_TYPE(app::EVT_COMMAND_OnTreeEvent_SelChanged)
IMPLEMENT_DYNAMIC_CLASS(TTreeEvent, wxNotifyEvent)

// TTreeCtrl //////////////////////////////////////////////////////////////////

//
TTreeCtrl::TTreeCtrl(wxWindow *parent
	,	prs::TDataTables &DataTables_
	, wxWindowID id
	, const wxPoint& pos
	, const wxSize& size
	, long style													
	, const wxValidator &validator
	, const wxString& name
)
: wxTreeCtrl( parent, id, pos, size, style | wxTR_HIDE_ROOT, validator, name)
,	DataTables(DataTables_)
, dataTree(NULL) 
{
	Connect( wxEVT_COMMAND_TREE_SEL_CHANGED,    wxTreeEventHandler( TTreeCtrl::OnTreeSelChanged ),    NULL, this );
  Connect( wxEVT_COMMAND_TREE_ITEM_EXPANDING, wxTreeEventHandler( TTreeCtrl::OnTreeItemExpanding ), NULL, this );
	Connect( wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxTreeEventHandler( TTreeCtrl::OnTreeItemCollapsed ), NULL, this );
}

//
void TTreeCtrl::LoadChildren(wxTreeItemId &Parent) {
	TTreeItemData *ParentData = GetItemData(Parent);

	for( int i = 0; i < ParentData->DataTreeRecord.GetChildrenCount(); ++i ) {
		prs::TDataTreeRecord ChildRecord = ParentData->DataTreeRecord.GetChild(i);
		wxTreeItemId ChildItem = AppendItem( Parent, ChildRecord.GetItemName().S(), 0, 0, new TTreeItemData(ChildRecord) );
		
		if (ChildRecord.GetChildrenCount()) {
			SetItemHasChildren( ChildItem );
		}
	}
}

//
void TTreeCtrl::UnloadChildren(wxTreeItemId &Parent) {
	DeleteChildren(Parent);
	TTreeItemData *ParentData = GetItemData(Parent);
	if (ParentData->DataTreeRecord.GetChildrenCount()) {
		SetItemHasChildren( Parent );
	}
}

//
void TTreeCtrl::ConnectData(prs::TDataTree* DataTree) {
	dataTree = DataTree;
	/**
	wxImageList* imglist = new wxImageList(16, 16, true, 2);
    imglist->Add(wxArtProvider::GetBitmap(wxART_FOLDER, wxART_OTHER, wxSize(16,16)));
    imglist->Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE, wxART_OTHER, wxSize(16,16)));
    tree->AssignImageList(imglist);
	/**/
	
	prs::TDataTreeRecord RootRecord	= dataTree->GetRecord(1);
	wxTreeItemId RootItem = AddRoot( 
		RootRecord.GetItemName().S(),
		0, 0, 
		new TTreeItemData( RootRecord )
	);
	//SetItemHasChildren( RootItem );
	LoadChildren(RootItem);
	//Expand(root);
}

//
void TTreeCtrl::OnTreeSelChanged(wxTreeEvent& event) {
	//wxObject *Sender = event.GetEventObject();
	//wxTreeItemId TreeItemId = event.GetItem();
	TTreeItemData *TreeItemData = GetItemData( event.GetItem() );

	TTreeEvent Event(
		EVT_COMMAND_OnTreeEvent_SelChanged, GetId()
	);
	Event.SetEventObject(this);

	if (!TreeItemData) {
		GetEventHandler()->ProcessEvent(Event);
		return;
	}

	Event.DataTreeRecord = &TreeItemData->DataTreeRecord;

	prs::TDataTable *ItemToTable = Event.DataTreeRecord->GetItemToTable();
	prs::TDataTableRecord *ItemToRecord = Event.DataTreeRecord->GetItemToRecord();
	
	svl::TString ItemToTableId = ItemToTable->TableId;
	int ItemToRecordId = ItemToRecord->RecordId;

	if ( ItemToTableId.CompareIC(TSystemTables::RegTables.GetId()) ) {
		prs::TDataRegTables DataRegTables( DataTables,TSystemTables::RegTables );

		prs::TDataRegTablesRecord DataRegTablesRecord = DataRegTables.GetRecord(ItemToRecordId);
		Event.DataRegTablesRecord = &DataRegTablesRecord;
		
		GetEventHandler()->ProcessEvent(Event);
	}
	else if ( ItemToTableId.CompareIC(TSystemTables::Folders.GetId()) ) {
		prs::TDataFolders DataFolders( DataTables,TSystemTables::Folders );

		prs::TDataFoldersRecord DataFoldersRecord = DataFolders.GetRecord(ItemToRecordId);
		Event.DataFoldersRecord = &DataFoldersRecord;
		
		GetEventHandler()->ProcessEvent(Event);
	}
	else {
		GetEventHandler()->ProcessEvent(Event);
	}

/****
  svl::TMessageModal(svl::TString("")
    << "\r\n:\r\n" << Event.Record->GetItemToRecord()->GetInfoAsString()
  );
*******/
}

//
void TTreeCtrl::OnTreeItemExpanding(wxTreeEvent& event) {
	LoadChildren( event.GetItem() );
}

//
void TTreeCtrl::OnTreeItemCollapsed(wxTreeEvent& event) {
	UnloadChildren( event.GetItem() );
}
